﻿using System;
using System.Collections.Generic;
using System.Text;
using YouMa.Model;
using System.Linq;

namespace YouMa.Reposity
{
    public class MessageReposity:ReposityBase
    {
        public MessageReposity(YouMaContext _db) : base(_db)
        {
            

        }

        //
        public PageListBase<Message> GetIndexMsgList(int pagesize, int pagenum)
        {
            List<Message> list = null;

            var query = from mes in db.Messages
                        orderby mes.CreateTime descending
                        select mes;

            list=query.Skip(pagesize*(pagenum - 1)).Take(pagesize).ToList();

            PageListBase<Message> PageList = new PageListBase<Message>()
            {
                PageNum = pagenum,
                PageSize = pagesize,
                TotalCount = query.Count(),
                List = list,
            };

            return PageList;
        }

        public PageListBase<Message> GetGZMsgList(int userid, int pagesize, int pagenum)
        {
            List<Message> list = null;

            var query = from mes in db.Messages
                        join guanzhu in db.GuanZhus on mes.ThemeId equals guanzhu.ThemeId
                        where guanzhu.UserId== userid
                        orderby mes.CreateTime descending
                        select mes;

            list = query.Skip(pagesize * (pagenum - 1)).Take(pagesize).ToList();

            PageListBase<Message> PageList = new PageListBase<Message>()
            {
                PageNum = pagenum,
                PageSize = pagesize,
                TotalCount = query.Count(),
                List = list,
            };

            return PageList;
        }

        public List<Message> GetSCMsgList(int userid)
        {
            var list = (from shoucang in db.ShouCangs
                       join mes in db.Messages on shoucang.MsgId equals mes.Id
                       where shoucang.UserId == userid
                       select mes).ToList();
            return list;
        }

        public List<Message> GetMsgAndUNList()
        {
            var query = from mes in db.Messages
                        join user in db.Users on mes.UserId equals user.Id
                        orderby mes.CreateTime descending
                        select mes;

            var list = query.Take(10).ToList();
            return list;
        }
    }
}
